var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/cmd/prettier.ts
var prettier_exports = {};
__export(prettier_exports, {
  check: () => check,
  default: () => prettier_default,
  format: () => format
});
module.exports = __toCommonJS(prettier_exports);
var utils = __toESM(require("@umijs/utils"));
var import_fs = require("fs");
var import_path = require("path");
var import_chalk = __toESM(require("chalk"));
var import_prettier = __toESM(require("prettier"));
var parserMap = {
  ".ts": "babel-ts",
  ".tsx": "babel-ts",
  ".css": "css",
  ".less": "less",
  ".scss": "scss",
  ".md": "markdown",
  ".mdx": "mdx",
  ".json": "json",
  ".html": "html"
};
var format = async ({
  cwd = process.cwd(),
  pattern = "src/**/*.{js,jsx,ts,tsx,css,less,scss,md,mdx,json,html}"
}) => {
  const configFilePath = (0, import_path.join)(cwd, ".prettierrc.js");
  const options = await import_prettier.default.resolveConfig(configFilePath);
  const files = utils.glob.sync(pattern, {
    cwd
  });
  files.forEach((file) => {
    const filePath = (0, import_path.join)(cwd, file);
    const ext = (0, import_path.extname)(filePath);
    const source = (0, import_fs.readFileSync)(filePath, { encoding: "utf-8" });
    const formatted = import_prettier.default.format(source, {
      ...options,
      parser: parserMap[ext] || "babel"
    });
    (0, import_fs.writeFileSync)(filePath, formatted);
  });
};
var check = async ({
  cwd = process.cwd(),
  pattern = "src/**/*.{js,jsx,ts,tsx,css,less,scss,md,mdx,json,html}"
}) => {
  const configFilePath = (0, import_path.join)(cwd, ".prettierrc.js");
  const options = await import_prettier.default.resolveConfig(configFilePath);
  const files = utils.glob.sync(pattern, {
    cwd
  });
  const notCheckedFiles = files.filter((file) => {
    const filePath = (0, import_path.join)(cwd, file);
    const ext = (0, import_path.extname)(filePath);
    const source = (0, import_fs.readFileSync)(filePath, { encoding: "utf-8" });
    return !import_prettier.default.check(source, {
      ...options,
      parser: parserMap[ext] || "babel"
    });
  });
  if (notCheckedFiles.length) {
    let message = "";
    notCheckedFiles.forEach((file) => {
      message += `${import_chalk.default.underline((0, import_path.join)(cwd, file))}
`;
    });
    message += import_chalk.default.red.bold(`
\u2716 ${notCheckedFiles.length} files have not been formatted with Prettier.
`);
    console.log(message);
    process.exitCode = 1;
  }
};
var registerPrettier = (api) => {
  api.registerCommand({
    name: "write",
    description: "format js,jsx,ts,tsx,css,less,scss,md,mdx,json,html code with prettier",
    details: `
$ umi write
    `,
    async fn() {
      const { cwd } = api.paths;
      format({ cwd });
    }
  });
  api.registerCommand({
    name: "prettier",
    description: "check or format js,jsx,ts,tsx,css,less,scss,md,mdx,json,html code with prettier",
    details: `
$ umi prettier
$ umi prettier --write
* write \u683C\u5F0F\u5316\u4EE3\u7801
    `,
    async fn({ args }) {
      const { cwd } = api.paths;
      const { write = false } = args;
      if (write)
        format({ cwd });
      else
        check({ cwd });
    }
  });
};
var prettier_default = registerPrettier;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  check,
  format
});
